﻿layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer;

    var ztree;
    var setting = {
        view: {
            checkable: true,
            selectedMulti: false
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: { "Y": "ps", "N": "ps" },
        },
        data: {
            key: {
                children: "children",
                name: "name",
            }
        }
        //async: {
        //    enable:true,
        //    url: '/Manage/GetAllPermissions',
        //    dataType: "text",
        //    autoParam: ["id", "name", "code", "nodes"]
        //}
    };
    var zTreeNodes = new Array();

    $(".display-per").click(function () {
        var id = $(this).prevAll('input[type="hidden"]').val();

        layer.open({
            type: 1,
            title: '权限列表',
            content: "<div class='ztree' id='permissionstree' style='margin:20px;'></div>",
            btn: ['提交', '取消'],
            shadeClose: true,
            yes: function () {
                var ids = GetSelectPermissions("permissionstree");
                var index = layer.load();
                $.ajax({
                    url: '/Manage/ModifyUserPermissions',
                    method: "post",
                    data: {
                        permissions: ids,
                    },
                    success: function (data) {
                        if (data.success != "") {
                            layer.msg(data.success);
                        }
                        else {
                            layer.msg(data.error);
                        }
                        layer.close(index);
                    },
                    error: function () {
                        layer.msg("服务端连接异常");
                        layer.close(index);
                    }
                })
            },
            btn2: function () {
                layer.closeAll();
            }
        });
        $.ajax({
            url: '/Manage/GetEmpolyeePermissions',
            type: 'Get',
            dataType: 'json',
            data: { employeeId: id },
            success: function (data) {
                //for(var i = 0;i < data.length;i++){
                //    zTreeNodes.push(data[i]);
                //}
                zTreeNodes = data;
                ztree = $.fn.zTree.init($("#permisstionstree"), setting, zTreeNodes);
            }
        })
    });

    $(".display-role").click(function () {
        var id = $(this).prevAll('input[type="hidden"]').val();
        layer.open({
            type: 1,
            title: '角色列表',
            btn: ['提交', '取消'],
            content: "<div class='ztree' id='permissionstree' style='margin:20px;'></div>",
            shadeClose: true,
            yes: function () {
                var ids = GetSelectPermissions("roletree");
                var index = layer.load();
                $.ajax({
                    url: '/Manage/ModifyUserRoles',
                    method: "post",
                    data: {
                        userId: id,
                        roles: ids,
                    },
                    success: function (data) {
                        if (data.success != "") {
                            layer.msg(data.success);
                        }
                        else {
                            layer.msg(data.error);
                        }
                        layer.close(index);
                    },
                    error: function () {
                        layer.msg("服务端连接异常");
                        layer.close(index);
                    }
                })
            },
            btn2: function () {
                layer.closeAll();
            }
        });
        $.ajax({
            url: '/Manage/GetEmpolyeePermissions',
            type: 'Get',
            dataType: 'json',
            data: { employeeId: id },
            success: function (data) {
                //for(var i = 0;i < data.length;i++){
                //    zTreeNodes.push(data[i]);
                //}
                zTreeNodes = data;
                ztree = $.fn.zTree.init($("#permisstionstree"), setting, zTreeNodes);
            }
        })

    })

    $(".delete-eml").click(function () {
        var id = $(this).prevAll('input[type="hidden"]').val();
        var root = $(this).parent().parent();
        $.ajax({
            url: "/Manage/DeleteEmployee",
            method: "post",
            data:{employeeId:id},
            success: function (data) {
                if (data.success != "") {
                    layer.msg(data.success);
                    root.remove();
                }
                else {
                    layer.msg(data.error);
                }
            },
            error: function () {
                layer.msg("服务端异常");
            }
        })
    })

    
})


function GetSelectPermissions(tag) {
    var treeObj = $.fn.zTree.getZTreeObj(tag);
    var nodes = treeObj.getCheckedNodes(true);

    var permissionIds = new Array();
    for (var i = 0; i < nodes.length; i++) {
        permissionIds.push(nodes[i].id);
    }
    return permissionIds;
}